package com.sun.designpatternsdemo.chainofresopnsibility

import android.util.Log

/**
 * 版权：xx公司 版权所有
 * @author sun
 * 版本：1.0
 * 创建日期：2021/7/1 16:58
 * 描述：类
 *创建抽象的记录器类。
 */
abstract class AbstractLogger {
    companion object {
        val INFO: Int = 1
        val DEBUG: Int = 2
        val ERROR: Int = 3
    }

    protected var level: Int = 0
    var nextLogger: AbstractLogger? = null
        set(nextLogger) {
            field = nextLogger
        }

    fun logMessage(level: Int, message: String) {
        if (this.level < level) {
            write(message)
        }
        //递归效果，不断调用下一级logmessage
        if (nextLogger != null) {
            nextLogger?.logMessage(level, message)
        }
    }

    abstract fun write(message: String)
}

class ConsoleLogger(var mlevel: Int): AbstractLogger() {
    init {
        this.level = mlevel
    }
    override fun write(message: String) {
        Log.e("---", "Standard Console::Logger  $message")
    }

}

class ErrorLogger(var mlevel: Int): AbstractLogger() {
    init {
        this.level = mlevel
    }
    override fun write(message: String) {
        Log.e("---", "Error Console::Logger  $message")
    }

}

class FileLogger(var mlevel: Int): AbstractLogger() {
    init {
        this.level = mlevel
    }
    override fun write(message: String) {
        Log.e("---", "File Console::Logger  $message")
    }

}